/*
 * @Author: your name
 * @Date: 2021-01-08 18:56:27
 * @LastEditTime: 2021-11-05 16:01:41
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vant\src\utils\request.js
 */
/**
 * axios 封装
 * @author maybe
 */
import axios from "axios";
import { Toast } from "vant";
import { getToken } from "@/utils/auth";
import router from "@/router";
import config from '../../config'
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8';
/**
 * 请求失败后的错误统一处理
 * @param {number} code 请求失败的状态码
 */
const handleError = (code) => {
  // switch (code) {
  //   case 401:
  //     // do something ...
  //     // 这里要清除一些全局的loading哦，例如 vant: Toast.clear()
  //     router.push("/Login");
  //     break;

  //   default:
  //     break;
  // }
  Toast.fail("网络异常，请检查后重试。")
};
// 创建axios实例
// create an axios instance
const service = axios.create({
  // withCredentials: true, // send cookies when cross-domain requests
  baseURL: config.baseApi, // .env中配置的api前缀
  timeout: 3600*10, // request timeout
});
/**
 * 请求拦截器
 * 每次请求前，如果存在token则在请求头中携带token
 */
service.interceptors.request.use(
  (config) => {
    // 获取token
    // console.log("1111")
    const token = window.localStorage.getItem("token");
    config.headers.token = token;
    
    // config.headers.token = "5b3d2466-add0-4347-a67e-a624afd44ebc";
    // console.log("config.headers"+config.headers);
    return config;
  },
  (error) => {
    // do something with request error
    // console.log(error, "err");
    // return Promise.reject(new error("网络异常，请检查后重试。"));
    Toast.fail("网络异常，请检查后重试。")
  }
);
// service.interceptors.requestByArr(
//   (config) => {
//     // 获取token
//     // console.log("1111")
//     const token = window.localStorage.getItem("token");
//     // console.log("token2:"+token);
//     config.headers.token = token;
//     config.responseType = 'arraybuffer';
//     // config.headers.token = "b5688f89-5e3e-4fa0-a44a-b52918c4e74b";
//     // console.log("config.headers"+config.headers);
//     return config;
//   },
//   (error) => {
//     // do something with request error
//     console.log(error, "err");
//     return Promise.reject(error);
//   }
// );
/**
 * 响应格式
 * { code => 状态码, msg => '响应信息', data => 数据 }
 */
service.interceptors.response.use(
  (response) => {
     response.data.data = JSON.parse(response.data.data); 
     const res = response.data
     const ress = response.data.data
    // console.log(ress);
    console.log('切面参数code：'+res.code)
    if (res.code != 200) {
      // 处理自定义错误
      // console.log(1)
      handleError(res.code);

      return Promise.reject(new Error("网络异常，请检查后重试。"));
    } else {
      // success
      // console.log(2)
      return Promise.resolve(res);
    }
  },
  (error) => {
    // console.log(3)
    // console.error("response error:" + error); // for debug
    Toast.fail({
      message: "网络异常，请检查后重试。",
      duration: 1.5 * 1000,
    });
    return Promise.reject(error);
  }
);

export default service;
